//package com.dragon.aipictures.manager.sharding.test2;
//
//import com.dragon.aipictures.domain.entity.Picture;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.stereotype.Component;
//
//@Aspect
//@Component
//@Order(1) // 确保在事务提交前执行
//public class DoubleWriteAspect {
//    @Autowired
//    private ShardMetadataService metadataService;
//    @Autowired
//    private JdbcTemplate jdbcTemplate;
//
//    @Around("execution(* com.example.repository.PictureRepository.save(..)) && args(picture)")
//    public Object handleDoubleWrite(ProceedingJoinPoint pjp, Picture picture) throws Throwable {
//        Object result = pjp.proceed(); // 原始写入
//
//        ShardMetadata metadata = metadataService.getMetadata(picture.getSpaceId());
//        if ("MIGRATING".equals(metadata.getStatus())) {
//            int shardIndex = (int) (picture.getId() % 4);
//            String tableName = String.format("picture_%d_%d",
//                picture.getSpaceId(), shardIndex);
//
//            jdbcTemplate.update(
//                "INSERT INTO " + tableName + " (id, space_id, content, created_at) "
//                + "VALUES (?, ?, ?, ?)",
//                picture.getId(),
//                picture.getSpaceId()
//            );
//        }
//        return result;
//    }
//}